﻿@{
    ViewBag.Title = "Organization_Tree";
    Layout = "~/Views/Shared/_LayoutChild.cshtml";
}
@section head
{
    <link href="@Url.Content("~/Content/zTreeStyle/zTreeStyle.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery.ztree.all-3.0.min.js")" type="text/javascript"></script>
    @Html.Partial("_JqGrid")
    <script language="javascript" type="text/javascript">
        var selectId = "";
        var selectrow = 1; //选中的行，默认为1
        var gridId = "#tableId";
        var curSelectNode = null;
        $(document).ready(function () {
            loadBaseDicTree('/SystemManage/Organization/GetTreeNode/', 'organization_id', 'organization_parentid', 'organization_name', true);
            funGridInit();

        });
        function zTreeOnClick(event, treeId, treeNode, clickFlag) {
            curSelectNode = treeNode;
            selectId = treeNode.organization_id;
            _zTreeObj.expandNode(treeNode, true); //展开点击节点
            Search();
        }

        function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
            if (curSelectNode == null) {//如果加载完，没有选中节点，则默认选中第一个节点
                var nodes = _zTreeObj.getNodes();
                if (nodes.length > 0) {
                    _zTreeObj.selectNode(nodes[0]);
                    curSelectNode = nodes[0];
                    selectId = nodes[0].organization_id;
                }
            }
            if(treeNode!=null){
                curSelectNode = treeNode;
                selectId = treeNode.organization_id;
            }
            zTreeOnClick(event, treeId, curSelectNode, 1);
//            $(gridId).trigger("reloadGrid");
        }
        function showdialog() { }

        function Search() {
            //查询
            $(gridId).setGridParam({ page: 1 });
            $(gridId).trigger("reloadGrid");
        }

        function editNode(type) {
            if (selectId == "") {
                parent.asyncbox.alert('请选择一个节点!', '提示');
                return;
            }
            if (type == "edit" && selectId == "0") {
                parent.asyncbox.alert('请选择一个节点!', '提示');
                return;
            }
            var u = "/SystemManage/Organization/Edit?id=&parentid=" + selectId;
            if (type == "edit")
                u = "/SystemManage/Organization/Edit?id=" + selectId;
            parent.asyncbox.open({
                url: u,
                data: '',
                title: type == "add" ? "新增" : "编辑",
                width: 520,
                height: 350,
                scroll: 'no',
                modal: true,
                btnsbar: parent.$.btn.OKCANCEL, //按钮栏配置请参考 “辅助函数” 中的 $.btn。
                callback: function (action) {
                    var t = this;
                    if (action == 'ok') {
                        DialogFrame($(this).attr("id")).Save(t);
                        return false;

                    }
                    if (action == 'cancel') {
                    }
                    if (action == 'close') {
                    }
                }
            });
        }

        function refreshTreeNode(type) {
            var nodes = _zTreeObj.getSelectedNodes();
            if (nodes.length <= 0) return;
            if (type == "add") {
                nodes[0].isParent = true;
                _zTreeObj.reAsyncChildNodes(nodes[0], "refresh");
            }
            else {
                _zTreeObj.reAsyncChildNodes(nodes[0].getParentNode(), "refresh");
            }
        }

        function deleteNode() {

            var pNode = curSelectNode.getParentNode();
            if (curSelectNode.isParent) {
                parent.asyncbox.alert('请删除该节点下的所有子节点后删除!', "提示");
                return false;
            }
            var childNum = pNode.children.length; //子节点的个数
            top.asyncbox.confirm('确认要删除该节点吗 ?', '', function (action) {
                //confirm 返回三个 action 值，分别是 'ok'、'cancel' 和 'close'。
                if (action == 'ok') {
                    $.funDelete("/SystemManage/Organization/Delete", { id: selectId, pid: pNode.organization_id, childNum: childNum }, function (d) {
                        if (d == "1") {
                            parent.asyncbox.tips('删除成功!', 'success');
                            if (childNum - 1 == 0) {
                                pNode.isParent = false;
                                _zTreeObj.reAsyncChildNodes(pNode.getParentNode(), "refresh");
                            }
                            _zTreeObj.reAsyncChildNodes(pNode, "refresh");
                            zTreeOnClick('', _zTreeObj, pNode, 1); //因该节点已删除 ，只能刷新父节点。

                        }
                        else {
                            parent.asyncbox.tips('删除失败:' +d.ErrorMessage, 'error');
                        }
                    });

                }
                if (action == 'cancel') {
                }
                if (action == 'close') {
                }
            });
        }
        function funGridInit() {
            //初始化表格
            var option = {
                height: $("#divGridContainer").height() - 51,
                width: $("#divGridContainer").width() - 2,
                url: '@Url.Content("~/SystemManage/Organization/GetOrganizationList")',
                autowidth: false,
                datatype: "json",
                sortname: 'Organization_Code',
                sortorder: 'asc',
                colModel: [
                { label: '单位编号', name: 'Organization_Code', width: 100 },
                { label: '单位名称', name: 'Organization_Name', width: 180 },
                { label: '单位类型', name: 'Organization_Type', width: 60, align: 'center', formatter: 'select', editoptions: { value: "0:管理部门;1:企业部门"} },
                { label: '龙头企业级别', name: 'Organization_Level', width: 120, align: 'center', formatter: 'select', editoptions: { value: "0:无;1:国家级;2:省级;3:市级"} },
                { label: '是否休闲农业', name: 'Organization_Leisure', width: 100, align: 'center', formatter: 'select', editoptions: { value: "0:否;1:是"} },
                { label: '单位地址', name: 'Organization_Address', width: 160 },
                { label: '是否明细', name: 'Organization_IsDetail', width: 100, align: 'center', formatter: 'YNFmatter' },
                { label: '联系人', name: 'Organization_Contact', width: 120 },
                { hidden: true, name: 'Organization_ID' }
   	            ],
                onSelectRow: function (rowid, cellname, value, iRow, iCol) {
                    selectId = $(gridId).getCell(rowid, "Organization_ID");
                    selectrow = rowid;
                    //同步树
                    curSelectNode = _zTreeObj.getNodeByParam("organization_id", selectId, null);
                    if (curSelectNode == null) return;
                    _zTreeObj.selectNode(curSelectNode);

                },
                ondblClickRow: function (rowid) {
                    if (rowid >= 0) {
                        selectId = $(gridId).getCell(rowid, "Organization_ID");
                        selectrow = rowid;
                        editNode('edit');

                    }
                },
                beforeRequest: function (d) {
                    if (curSelectNode == null) return false;
                    var data = { organizeId: curSelectNode == null ? '' : curSelectNode.organization_id };
                    $(gridId).jqGrid('setGridParam', { postData: data });
                },
                gridComplete: function () {
                    if ($(gridId).jqGrid('getGridParam', 'reccount') > 0)//取得记录行数，如果大于零，则选中上次选中的行
                    {
                        if ($(gridId).getDataIDs().length >= selectrow) {
                            $(gridId).setSelection(selectrow, true);
                        }
                        else
                            $(gridId).setSelection(1, true);

                    }
                }
            };
            var o = jqGridHelper.getConfig("#pageId", option);
            $(gridId).jqGrid(o);
        }
    </script>
}
<!--头部导航,不用时可以去掉-->
<div class="ui-layout-north">
    <div id="headertitle">
    </div>
    <div class="buttonlist" style="float: right; height: 34px; line-height: 36px; margin-right: 20px;">
      <a  onclick="editNode('add')" class="button green"><span class="icon-botton" style="background: url('/Content/16/add.png') no-repeat scroll 0px 4px;"></span>新增</a>
        <a  onclick="editNode('edit')" class="button green"><span class="icon-botton" style="background: url('/Content/16/edit.png') no-repeat scroll 0px 4px;"></span>编辑</a>
        <a  onclick="deleteNode()" class="button green"><span class="icon-botton" style="background: url('/Content/16/delete.png') no-repeat scroll 0px 4px;"></span>删除</a>
      </div>
</div>
<!--左侧导航，不用时直接去掉-->
<div class="ui-layout-west">
    @Html.Partial("_BaseDicTree")
</div>
<div id="divGridContainer" class="ui-layout-center" style="overflow: auto;">
    <table id="tableId">
    </table>
    <div id="pageId">
    </div>
</div>
<!--主窗体，页面都需要的div-->
<div class="ui-layout-center" style="overflow: auto;">
</div>
